package com.prince.web.controller;

import com.prince.aspect.AppLog;
import com.prince.bean.CommonWebResponse;
import com.prince.bean.ResultParams;
import com.prince.constant.Constants;
import com.prince.model.User;
import com.prince.util.StringUtil;
import com.prince.web.base.SessionUtil;
import com.prince.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * 用户登陆
 * @author Wangpeixi
 *
 */
@Controller
public class UserLoginController {


	@Value("${aeadTime}")
	protected Integer aeadTime;
	@Autowired
	private UserService userService;
	/**
	 * 用户登录
	 * 
	 * @param user
	 * @return 
	 * @return
	 * @throws IOException 
	 */
	@AppLog
	@RequestMapping(value = "userLogin", method = RequestMethod.POST)
	public void userLogin(User user, HttpServletRequest request,HttpServletResponse resp) throws IOException {
		if(StringUtil.isEmpty(user.getLinkman()) || StringUtil.isEmpty(user.getPwd())) {
			resp.getWriter().println("用户名或密码不能为空!");
			return;
		}
		String requestURL = request.getRequestURL().toString();
		// 获取登录ip
		user.setLastLoginIp(request.getRemoteAddr());
		// 查询用户
		Map<String, Object> map = userService.login(user,requestURL);
		if (StringUtil.stringEquals("00", map.get("returnCode").toString())) {
			resp.getWriter().println("/view/adminIndex.html");
			return;
		}else{
			resp.getWriter().println(map.get("returnMsg"));
			return;
		}
	}

	/**
	 * 退出登录
	 *
	 * @return
	 * @throws IOException 
	 */
	@AppLog
	@RequestMapping(value = "exitLogin", method = RequestMethod.POST)
	public ResponseEntity<Map<String, Object>> exitLogin(HttpServletResponse resp) throws IOException {
		ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
		User user = SessionUtil.getSession();
		if (user == null) {
			resp.getWriter().println("/view/adminIndex.html");
			return null;
		}
		SessionUtil.invalidate();
		return new ResponseEntity<Map<String, Object>>(response.toHashMap(), HttpStatus.OK);
	}

	/**
	 * 查询登录成功的名称
	 * 
	 * @param request
	 * @return
	 */
	@AppLog
	@RequestMapping(value = "findUserName", method = RequestMethod.POST)
	public ResponseEntity<Map<String, Object>> findUserName(HttpServletRequest request) {
		ResultParams response = CommonWebResponse.getSuccessResponseCodeMsgParams();
		User user = SessionUtil.getSession();
		if (null == user) {
			return null;
		}
		response.put("name", user.getLoginName());
		response.put("type", user.getBelongType());
		return new ResponseEntity<Map<String, Object>>(response.toHashMap(), HttpStatus.OK);
	}
	

}